package com.ren.newvideoplayer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ren.newvideoplayer.R;
import com.ren.newvideoplayer.module.Video;
import com.ren.newvideoplayer.player.NiceUtil;
import com.ren.newvideoplayer.player.NiceVideoPlayer;
import com.ren.newvideoplayer.player.NiceVideoPlayerController;

import java.util.List;

/**
 * Created by ren on 2017/10/20.
 */

public class VideoPlayerRecyclerAdapter extends RecyclerView.Adapter<VideoPlayerRecyclerAdapter.MyViewHolder> {
    private Context mContext;
    private List<Video> mDataList;

    public VideoPlayerRecyclerAdapter(Context context,List<Video> dataList){
        this.mContext = context;
        this.mDataList = dataList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_video, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(itemView);
        NiceVideoPlayerController controller = new NiceVideoPlayerController(mContext);
        myViewHolder.setController(controller);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.bindData(mDataList.get(position));
    }

    @Override
    public int getItemCount() {
        return mDataList == null? 0 : mDataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        private NiceVideoPlayer mVideoPlayer;
        private NiceVideoPlayerController mController;

        public MyViewHolder(View itemView) {
            super(itemView);
            mVideoPlayer = (NiceVideoPlayer) itemView.findViewById(R.id.nice_video_player);

            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mVideoPlayer.getLayoutParams();
            lp.width = NiceUtil.getScreenWidth(mContext);
            lp.height = (NiceUtil.getScreenWidth(mContext) * 9)/16;
            mVideoPlayer.setLayoutParams(lp);
        }
        public void setController(NiceVideoPlayerController controller) {
            mController = controller;
        }

        public void bindData(Video video) {
            mController.setTitle(video.getTitle());
            mController.setImage(video.getImageUrl());
            mVideoPlayer.setController(mController);
            mVideoPlayer.setUp(video.getVideoUrl(), null);
        }
    }

}
